﻿/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;

namespace IvyOrm
{
    class IvyColumnInfo
    {
        public bool IncludeInInsert;
        public Type DatabaseType { get; set; }
        public ValueConverterFactory ConverterFactory { get; set; }
        public Type FieldOrPropertyType { get; set; }
        public MemberInfo Member { get; set; }
        public bool PrimaryKey { get; set; }
        public bool PrimaryKeyReturn { get; set; }
        public string ColumnName { get; set; }
        
        public void SetValueConverted(object obj, object convertedValue)
        {
            var memberAsFieldInfo = Member as FieldInfo;
            var memberAsPropertyInfo = Member as PropertyInfo;
            if (memberAsFieldInfo != null)
                memberAsFieldInfo.SetValue(obj, convertedValue);
            else
            {
                Debug.Assert(memberAsPropertyInfo != null);
                memberAsPropertyInfo.SetValue(obj, convertedValue, null);
            }
        }
        public object GetValueUnconverted(object obj)
        {
            object preconvertedValue;
            var memberAsFieldInfo = Member as FieldInfo;
            var memberAsPropertyInfo = Member as PropertyInfo;
            if (memberAsFieldInfo != null)
                preconvertedValue = memberAsFieldInfo.GetValue(obj);
            else
            {
                Debug.Assert(memberAsPropertyInfo != null);
                preconvertedValue = memberAsPropertyInfo.GetValue(obj, null);
            }
            return preconvertedValue;
        }
    }
}
